package com.example.web.service.impl;

import com.example.web.entity.InteractionRecord;
import com.example.web.entity.Customer;
import com.example.web.mapper.InteractionRecordMapper;
import com.example.web.mapper.CustomerMapper;
import com.example.web.service.InteractionRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InteractionRecordServiceImpl implements InteractionRecordService {

    @Autowired
    private InteractionRecordMapper interactionRecordMapper;
    
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public InteractionRecord findById(Long id) {
        if (id == null) {
            return null;
        }
        return interactionRecordMapper.findById(id);
    }

    @Override
    public Map<String, Object> getInteractionRecordList(Integer page, Integer size, 
                                                       String interactionType, 
                                                       Long customerId, 
                                                       Long creatorId) {
        // 设置默认值
        if (page == null || page < 1) {
            page = 1;
        }
        if (size == null || size < 1) {
            size = 10;
        }
        
        Integer offset = (page - 1) * size;
        
        // 查询列表数据
        List<InteractionRecord> records = interactionRecordMapper.findPage(
            offset, size, interactionType, customerId, creatorId);
        
        // 查询总数
        Integer total = interactionRecordMapper.countTotal(interactionType, customerId, creatorId);
        
        // 构建返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("records", records);
        result.put("total", total);
        result.put("page", page);
        result.put("size", size);
        result.put("pages", (total + size - 1) / size);
        
        return result;
    }

    @Override
    public InteractionRecord createInteractionRecord(InteractionRecord interactionRecord) {
        if (interactionRecord == null) {
            throw new RuntimeException("互动记录信息不能为空");
        }
        
        // 验证必填字段
        if (StringUtils.isBlank(interactionRecord.getInteractionType())) {
            throw new RuntimeException("互动类型不能为空");
        }
        if (StringUtils.isBlank(interactionRecord.getContentSummary())) {
            throw new RuntimeException("内容摘要不能为空");
        }
        if (interactionRecord.getCustomerId() == null) {
            throw new RuntimeException("关联客户不能为空");
        }
        if (interactionRecord.getCreatorId() == null) {
            throw new RuntimeException("创建人不能为空");
        }
        
        // 验证客户是否存在且属于当前用户
        Customer customer = customerMapper.findById(interactionRecord.getCustomerId());
        if (customer == null) {
            throw new RuntimeException("关联的客户不存在");
        }
        if (!customer.getCreatorId().equals(interactionRecord.getCreatorId())) {
            throw new RuntimeException("无权限操作该客户的互动记录");
        }
        
        // 设置创建时间
        interactionRecord.setCreateTime(LocalDateTime.now());
        interactionRecord.setUpdateTime(LocalDateTime.now());
        
        interactionRecordMapper.insert(interactionRecord);
        return interactionRecord;
    }

    @Override
    public InteractionRecord updateInteractionRecord(InteractionRecord interactionRecord) {
        if (interactionRecord == null || interactionRecord.getId() == null) {
            throw new RuntimeException("互动记录ID不能为空");
        }
        
        InteractionRecord existingRecord = interactionRecordMapper.findById(interactionRecord.getId());
        if (existingRecord == null) {
            throw new RuntimeException("互动记录不存在");
        }
        
        // 验证必填字段
        if (StringUtils.isBlank(interactionRecord.getInteractionType())) {
            throw new RuntimeException("互动类型不能为空");
        }
        if (StringUtils.isBlank(interactionRecord.getContentSummary())) {
            throw new RuntimeException("内容摘要不能为空");
        }
        if (interactionRecord.getCustomerId() == null) {
            throw new RuntimeException("关联客户不能为空");
        }
        
        // 验证权限：只能修改自己创建的记录
        if (!existingRecord.getCreatorId().equals(interactionRecord.getCreatorId())) {
            throw new RuntimeException("无权限修改该互动记录");
        }
        
        // 验证客户是否存在且属于当前用户
        Customer customer = customerMapper.findById(interactionRecord.getCustomerId());
        if (customer == null) {
            throw new RuntimeException("关联的客户不存在");
        }
        if (!customer.getCreatorId().equals(interactionRecord.getCreatorId())) {
            throw new RuntimeException("无权限操作该客户的互动记录");
        }
        
        // 设置更新时间
        interactionRecord.setUpdateTime(LocalDateTime.now());
        
        interactionRecordMapper.update(interactionRecord);
        return interactionRecord;
    }

    @Override
    public boolean deleteInteractionRecord(Long id) {
        if (id == null) {
            return false;
        }
        
        InteractionRecord record = interactionRecordMapper.findById(id);
        if (record == null) {
            return false;
        }
        
        interactionRecordMapper.deleteById(id);
        return true;
    }

    @Override
    public List<InteractionRecord> getInteractionRecordsByCustomer(Long customerId) {
        if (customerId == null) {
            return new ArrayList<>();
        }
        return interactionRecordMapper.findByCustomerId(customerId);
    }

    @Override
    public List<Customer> getCustomersByCreator(Long creatorId) {
        if (creatorId == null) {
            return new ArrayList<>();
        }
        return customerMapper.findByCreatorId(creatorId);
    }

    @Override
    public List<Map<String, String>> getInteractionTypeOptions() {
        List<Map<String, String>> options = new ArrayList<>();
        
        Map<String, String> phone = new HashMap<>();
        phone.put("label", "电话");
        phone.put("value", "电话");
        options.add(phone);
        
        Map<String, String> email = new HashMap<>();
        email.put("label", "邮件");
        email.put("value", "邮件");
        options.add(email);
        
        Map<String, String> meeting = new HashMap<>();
        meeting.put("label", "会议");
        meeting.put("value", "会议");
        options.add(meeting);
        
        return options;
    }
}